package demon1;

import java.io.*;
import java.util.Scanner;

public class excel2 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要搜索的路径");
        String path =scanner.next();
        System.out.println("请输入要查询的文件名");
        String searchWord=scanner.next();
        
        File rootFile=new File(path);
        if(!rootFile.isDirectory()){
            System.out.println("当前输入路径非法");
            return;
        }
        search(rootFile,searchWord);
    }

    private static void search(File rootFile, String searchWord) {
        File[] file=rootFile.listFiles();
        if(file==null){
            return;
        }
        for(File x:file){
            if(x.isFile()){
                mathWord(x,searchWord);
            }else if(x.isDirectory()){
                search(x,searchWord);
            }
        }
    }

    private static void mathWord(File x, String searchWord) {
        try(Reader reader=new FileReader(x)){
            StringBuffer stringBuffer=new StringBuffer();
            while(true){
                int c=reader.read();
                if(c==-1){
                    break;
                }
                stringBuffer.append((char)c);

            }
            if(stringBuffer.indexOf(searchWord)>=0){
                System.out.println(x.getAbsolutePath());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
